// components/calendar.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';

Component({

    /**
     * 组件的属性列表
     */
    properties: {
        activeColor: {
            type: String,
            value: '#ec7044',
        },
        visible: {
            type: Boolean,
            value: false,
        },
        value: {
            type: Object,
            value: {
                timerAmount: {
                    type: Array,
                    value: []
                },
                list: {
                    type: Array,
                    value: []
                },
                siteId: {
                    type: Number,
                    value: 0
                }
            },
            observer(value) {
                const timerAmount = value.timerAmount;
                // if (!timerAmount) {
                //     return;
                // }
                const date = new Date();
                const nowYear = date.getFullYear();
                const nowMonth = date.getMonth() + 1;
                this.setData({
                    year: nowYear,
                    month: nowMonth
                })
                let dateList = [{
                    year: nowYear,
                    month: nowMonth,
                    dates: this.getDates(nowYear, nowMonth, timerAmount)
                }];
                if (nowMonth == 11) {
                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 1,
                        dates: this.getDates(nowYear, nowMonth + 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear + 1,
                        month: 1,
                        dates: this.getDates(nowYear, 1, timerAmount)
                    })
                } else if (nowMonth == 12) {
                    dateList.push({
                        year: nowYear + 1,
                        month: 1,
                        dates: this.getDates(nowYear + 1, 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear + 1,
                        month: 2,
                        dates: this.getDates(nowYear, 2, timerAmount)
                    })
                } else {
                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 1,
                        dates: this.getDates(nowYear, nowMonth + 1, timerAmount)
                    })

                    dateList.push({
                        year: nowYear,
                        month: nowMonth + 2,
                        dates: this.getDates(nowYear, nowMonth + 2, timerAmount)
                    })
                }

                let list = value.list;
                if (list.length > 0) {
                    //year + '年' + month + '月' + day
                    for (let i = 0; i < dateList.length; i++) {
                        let monthDatas = dateList[i];
                        for (let j = 0; j < monthDatas.dates.length; j++) {
                            if (monthDatas.dates[j] != 0) {
                                let dayData = monthDatas.dates[j];
                                let timerDatas = dayData.timer;
                                for (let l = 0; l < timerDatas.length; l++) {
                                    let month = monthDatas.month;
                                    let day = dayData.day;
                                    if (month < 10) {
                                        month = '0' + month;
                                    }
                                    if (day < 10) {
                                        day = '0' + day;
                                    }
                                    let id = monthDatas.year + '' + month + '' + day + '' + timerDatas[l].id;
                                    let tag = 0;
                                    for (let m = 0; m < list.length; m++) {
                                        if (id === list[m].id) {
                                            dateList[i].dates[j].timer[l].selected = true;
                                            tag += 1;
                                        }
                                    }
                                    if (tag > 0) {
                                        dateList[i].dates[j].des = '已选'
                                    } else {
                                        dateList[i].dates[j].des = '可订'
                                    }
                                }
                            }
                        }
                    }
                }

                this.setData({
                    dateList
                })
                this.computeCount();
                if (value.siteId) {
                    this.getBookingTimeAppMonth(value.siteId, dateList);
                }
            },
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        dates: [],
        week: ['日', '一', '二', '三', '四', '五', '六'],
        selectedTimerIndex: 0,
        dateList: [],
        current: 0,
        count: 0,
        amount: 0
    },

    lifetimes: {
        created: function () {},
        attached: function () {},
        ready: function () {},
        detached: function () {},
    },

    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function () {
            this.list = [];
        },
        hide: function () {},
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getDates(year, month, timer) {
            var dates = [];
            var startDate = new Date(year, month - 1, 1);
            var endDate = new Date(year, month, 0);
            var dayOfWeek = startDate.getDay();
            for (var i = 0; i < dayOfWeek; i++) {
                dates.push(0);
            }

            const nowYear = this.data.year;
            const nowMonth = this.data.month;
            const date = new Date();
            const nowDay = date.getDate();

            if (month < 10) {
                month = '0' + month;
            }

            for (var i = startDate.getDate(); i <= endDate.getDate(); i++) {
                let day = i;
                if (day < 10) {
                    day = '0' + i;
                }
                let timerAmount = [{
                    id: 1,
                    mode: '上午',
                    timer: 1,
                    time: '09:00-12:00',
                    endTime: year + '-' + month + '-' + day + ' 12:00:00',
                    // amount: timer[0],
                    selected: false
                }, {
                    id: 2,
                    mode: '下午',
                    timer: 2,
                    time: '14:00-17:00',
                    endTime: year + '-' + month + '-' + day + ' 17:00:00',
                    // amount: timer[1],
                    selected: false
                }, {
                    id: 3,
                    mode: '晚间',
                    timer: 3,
                    time: '19:00-22:00',
                    endTime: year + '-' + month + '-' + day + ' 22:00:00',
                    // amount: timer[2],
                    selected: false
                }];

                if (nowYear == year && nowMonth == month) {
                    if (i == nowDay) {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'normal', // disable, full, normal, selecting, selected
                            day: i,
                            timer: timerAmount,
                            isToday: true,
                            des: '可订'
                        });
                    } else if (i < nowDay) {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'disable',
                            day: i,
                            timer: timerAmount,
                            isToday: false,
                            des: ''
                        });
                    } else {
                        dates.push({
                            id: year + '' + month + '' + day,
                            status: 'normal',
                            day: i,
                            timer: timerAmount,
                            isToday: false,
                            des: '可订'
                        });
                    }
                } else {
                    dates.push({
                        id: year + '' + month + '' + day,
                        status: 'normal',
                        day: i,
                        timer: timerAmount,
                        isToday: false,
                        des: '可订'
                    });
                }
            }
            // const arr = [];
            // for (let i = 0; i < dates.length; i += 7) {
            //     arr.push(dates.slice(i, i + 7));
            // }
            return dates;
        },

        getBookingTimeAppMonth(siteId, dateList) {
            let monthDate = dateList[this.data.current];
            network.getJson(api.timerMonth, {
                siteId,
                year: monthDate.year,
                month: monthDate.month
            }).then(res => {
                let data = res.data;
                for (let i = 0; i < dateList[this.data.current].dates.length; i++) {
                    for (let j = 0; j < data.length; j++) {
                        if (dateList[this.data.current].dates[i].day == data[j].date) {
                            for (let l = 0; l < dateList[this.data.current].dates[i].timer.length; l++) {
                                if (data[j].timer.indexOf(dateList[this.data.current].dates[i].timer[l].id) != -1) {
                                    dateList[this.data.current].dates[i].timer[l].disable = true;
                                }
                            }
                            if (dateList[this.data.current].dates[i].status != 'disable') {
                                if (data[j].timer.length == 5) {
                                    dateList[this.data.current].dates[i].status = 'full';
                                    dateList[this.data.current].dates[i].des = '已订满';
                                } else {
                                    dateList[this.data.current].dates[i].status = 'normal';
                                    dateList[this.data.current].dates[i].des = '可订';
                                }
                            }
                        }
                    }
                }
                this.setData({
                    [`dateList[${this.data.current}]`]: this.data.dateList[this.data.current]
                })
                this.computeCount();
            })
        },

        leftTap() {
            if (this.data.current == 0) {
                return;
            }
            this.setData({
                current: this.data.current - 1
            })
        },

        rightTap() {
            if (this.data.current == 5) {
                return;
            }
            this.setData({
                current: this.data.current + 1
            })
        },

        onVisibleChange(e) {
            this.setData({
                visible: e.detail.visible,
            });
        },

        handleSelect(e) {
            const index = e.currentTarget.dataset.index;
            if (this.data.dateList[this.data.current].dates[index].status === 'disable') {
                return;
            }
            if (this.data.dateList[this.data.current].dates[index].status === 'full') {
                return;
            }
            this.status = this.data.dateList[this.data.current].dates[index].status;
            if (this.status === 'selected') {
                this.setData({
                    [`dateList[${this.data.current}].dates[${index}].status`]: 'normal'
                })
                return;
            }
            for (let i = 0; i < this.data.dateList[this.data.current].dates.length; i++) {
                if (this.data.dateList[this.data.current].dates[i].status === 'selected') {
                    this.data.dateList[this.data.current].dates[i].status = 'normal';
                }
            }
            this.data.dateList[this.data.current].dates[index].status = 'selected';

            this.setData({
                selectedTimerIndex: index,
                [`dateList[${this.data.current}].dates`]: this.data.dateList[this.data.current].dates
            })
        },

        timerTap(e) {
            const index = e.detail.lastIndex;
            // let day = this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex];
            // let timer = day.timer[index];
            // let startTime = timer.time.split('-')[1];

            // let month = this.data.dateList[this.data.current].month;
            // if (month < 10) {
            //     month = '0' + month
            // }

            // let d = day.day;
            // if (d < 10) {
            //     d = '0' + d
            // }
            // let startDate = this.data.dateList[this.data.current].year + "-" + month + "-" + d + " " + startTime + ":00";
            // console.log(startDate)
            // if (new Date().getTime() >= new Date(startDate).getTime()) {
            //     wx.showToast({
            //         icon: 'none',
            //         title: '预订时间已过'
            //     })
            //     return;
            // }
            this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex].timer[index].selected = e.detail.lastSelected;
            let tag = 0;
            for (let j = 0; j < this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex].timer.length; j++) {
                if (this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex].timer[j].selected == true) {
                    tag += 1;
                }
            }
            if (tag > 0) {
                this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex].des = '已选';
            } else {
                this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex].des = '可订';
            }

            this.setData({
                [`dateList[${this.data.current}].dates[${this.data.selectedTimerIndex}]`]: this.data.dateList[this.data.current].dates[this.data.selectedTimerIndex]
            })
            this.computeCount();
        },

        computeCount() {
            let count = 0;
            let amount = 0;
            for (let i = 0; i < this.data.dateList.length; i++) {
                let monthDatas = this.data.dateList[i];
                for (let j = 0; j < monthDatas.dates.length; j++) {
                    if (monthDatas.dates[j] != 0) {
                        let dayData = monthDatas.dates[j];
                        let timer = dayData.timer;
                        for (let k = 0; k < timer.length; k++) {
                            if (timer[k].selected) {
                                count += 1;
                                // amount += timer[k].amount * 100
                            }
                        }
                    }
                }
            }
            this.setData({
                count,
                // amount: amount / 100
            })
        },

        submit() {
            let list = [];
            for (let i = 0; i < this.data.dateList.length; i++) {
                let monthDatas = this.data.dateList[i];
                for (let j = 0; j < monthDatas.dates.length; j++) {
                    if (monthDatas.dates[j] != 0) {
                        let dayData = monthDatas.dates[j];
                        let timer = dayData.timer;
                        for (let k = 0; k < timer.length; k++) {
                            if (timer[k].selected == true) {
                                let year = monthDatas.year;
                                let month = monthDatas.month;
                                let day = dayData.day;
                                if (month < 10) {
                                    month = '0' + month;
                                }
                                if (day < 10) {
                                    day = '0' + day;
                                }
                                let time = timer[k].time.split('-');
                                let startTime = time[0] + ':00';
                                let endTime = time[1] + ':00';

                                list.push({
                                    id: year + '' + month + '' + day + '' + timer[k].id,
                                    mode: timer[k].id,
                                    showTime: month + '月' + day + '日' + ' ' + time[0] + '-' + time[1],
                                    startTime: year + '-' + month + '-' + day + ' ' + startTime,
                                    endTime: year + '-' + month + '-' + day + ' ' + endTime,
                                    // amount: timer[k].amount
                                })
                            }
                        }
                    }
                }
            }
            if (list.length > 0) {
                this.setData({
                    visible: false
                });
            }
            this.triggerEvent('submit', list)
        }
    }
})